package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.bo.SettlementBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.SettlementMapper;
import com.liuzu.esys.pojo.Settlement;
import com.liuzu.esys.service.SettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service("settlementService")
public class SettlementServiceImpl implements SettlementService {

    @Autowired
    private SettlementMapper settlementMapper;

    @Override
    public LayuiPageVo<Settlement> settlementPageData(Map<String, Object> params) {

        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page,limit);
        List<Settlement> settlements = settlementMapper.selectByParams(params);
        PageInfo<Settlement> settlementPageInfo = new PageInfo<>(settlements);
        LayuiPageVo<Settlement> settlementLayuiPageVo = new LayuiPageVo<>();
        settlementLayuiPageVo.setCode(0);
        settlementLayuiPageVo.setMsg("分页列表数据");
        settlementLayuiPageVo.setCount(settlementPageInfo.getTotal());
        settlementLayuiPageVo.setData(settlementPageInfo.getList());

        return settlementLayuiPageVo;
    }

    @Override
    public int addSettlement(Settlement settlement) {
        int i = settlementMapper.addSettlement(settlement);
        return i;
    }

    @Override
    public int updateSettlement(Settlement settlement) {
        return settlementMapper.updateSettlementByKey(settlement);
    }

    @Override
    public int deleteSettlement(Integer order_number) {
        return settlementMapper.deleteSettlementByKey(order_number);
    }

    @Override
    public int deleteSettlements(String order_numbers) {
        String[] arrOrder_number = order_numbers.split(",");
        return settlementMapper.deleteSettlementsByKey(arrOrder_number);
    }

    @Override
    public List<SettlementBo> settlementExcelData(Settlement settlement) {
        List<Settlement> settlements = settlementMapper.selectByKey(settlement);
        List<SettlementBo> settlementBos = settlements.stream()
                .map((sr) ->{
                    SettlementBo settlementBo = new SettlementBo();
                    settlementBo.setOrder_number(sr.getOrder_number());
                    settlementBo.setId(sr.getId());
                    settlementBo.setName(sr.getName());
                    settlementBo.setPrice(sr.getPrice());
                    settlementBo.setBuy_num(sr.getBuy_num());
                    settlementBo.setPay_money(sr.getPay_money());
                    settlementBo.setSettlement_date(sr.getSettlement_date());
                    settlementBo.setSettlement_status(sr.getSettlement_status());
                    return settlementBo;
                }).collect(Collectors.toList());
        return settlementBos;
    }

}
